package com.laoniu.controller.sys;

import com.laoniu.annotation.OperateLog;
import com.laoniu.biz.RoleBiz;
import com.laoniu.dto.RestResult;
import com.laoniu.dto.request.RoleFormDto;
import com.laoniu.dto.request.RoleRequestDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/role")
public class RoleController {


    @Autowired
    private RoleBiz roleBiz;

    /**
     * 角色列表
     * @param requestDto
     * @return
     */
    @PostMapping("list")
    public RestResult list(@RequestBody RoleRequestDto requestDto){
        return RestResult.success(roleBiz.list(requestDto));
    }

    /**
     * 新增角色
     * @param dto
     * @return
     */
    @PostMapping("add")
    @OperateLog(bizName = "角色管理", operateType = "新增角色", operateDesc = "管理后台新增角色")
    public RestResult add(@RequestBody RoleFormDto dto){
        roleBiz.addRole(dto);
        return RestResult.success();
    }

    /**
     * 更新角色
     * @param dto
     * @return
     */
    @PostMapping("update")
    @OperateLog(bizName = "角色管理", operateType = "修改角色", operateDesc = "后台修改角色")
    public RestResult update(@RequestBody RoleFormDto dto){
        roleBiz.updateRole(dto);
        return RestResult.success();
    }

    /**
     * 删除角色
     * @param dto
     * @return
     */
    @PostMapping("delete")
    @OperateLog(bizName = "角色管理", operateDesc = "后台删除角色", operateType = "删除角色")
    public RestResult deleteRole(@RequestBody RoleFormDto dto){
        roleBiz.deleteById(dto);
        return RestResult.success();
    }

    /**
     * 获取已经选中的权限
     * @param dto
     * @return
     */
    @PostMapping("getRoleMenuIds")
    public RestResult getRoleMenuIds(@RequestBody RoleFormDto dto){
        return RestResult.success(roleBiz.getRoleMenuIds(dto.getId()));
    }

    /**
     * 保存角色菜单
     * @param dto
     * @return
     */
    @PostMapping("saveRoleMenu")
    @OperateLog(bizName = "角色管理", operateType = "角色菜单分配", operateDesc = "给角色分配菜单权限")
    public RestResult saveRoleMenu(@RequestBody RoleFormDto dto){
        roleBiz.saveRoleMenu(dto);
        return RestResult.success();
    }

    /**
     * 获取所有角色
     * @return
     */
    @GetMapping("roleList")
    public RestResult roleList(){
        return RestResult.success(roleBiz.roleList());
    }
}
